package com.xtiim.battery.discharge.persistence;


import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.io.InputStream;
import java.util.Properties;

/**
 * redis util
 */
public class RedisUtil {

    private static JedisPool jedisPool;

    static {

        try {
            InputStream inputStream = RedisUtil.class.getClassLoader().getResourceAsStream("application.properties");
            Properties properties = new Properties();

            properties.load(inputStream);
            String host = properties.getProperty("redis.host", "localhost");
            int port = Integer.parseInt(properties.getProperty("redis.port", "6379"));
            int maxTotal = Integer.parseInt(properties.getProperty("redis.maxTotal", "40"));
            int maxIdle = Integer.parseInt(properties.getProperty("redis.maxIdle", "20"));
            int minIdle = Integer.parseInt(properties.getProperty("redis.minIdle", "20"));

            JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
            jedisPoolConfig.setMaxTotal(maxTotal);
            jedisPoolConfig.setMaxIdle(maxIdle);
            jedisPoolConfig.setMinIdle(minIdle);

            jedisPool = new JedisPool(jedisPoolConfig, host, port);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Jedis getJedis() {
        return jedisPool.getResource();
    }
}


